﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Top.Api;
using Top.Api.Request;
using Top.Api.Response;

namespace Taobao.Open.Platform
{
    public class CainiaoPrintAPI
    {
        public string WaybillSearch(string waybillCode, string url, string appkey, string secret, string sessionKey)
        {
            ITopClient client = new DefaultTopClient(url, appkey, secret);
            CainiaoWaybillIiQueryByTradecodeRequest req = new CainiaoWaybillIiQueryByTradecodeRequest();
            List<CainiaoWaybillIiQueryByTradecodeRequest.WaybillDetailQueryByBizSubCodeRequestDomain> list2 = new List<CainiaoWaybillIiQueryByTradecodeRequest.WaybillDetailQueryByBizSubCodeRequestDomain>();
            CainiaoWaybillIiQueryByTradecodeRequest.WaybillDetailQueryByBizSubCodeRequestDomain obj3 = new CainiaoWaybillIiQueryByTradecodeRequest.WaybillDetailQueryByBizSubCodeRequestDomain();
            list2.Add(obj3);
            obj3.BizSubCode = waybillCode;
            obj3.ObjectId = "1";
            req.ParamList_ = list2;
            CainiaoWaybillIiQueryByTradecodeResponse rsp = client.Execute(req, sessionKey);
            return rsp.Body;
        }

        public string WaybillGet(CainiaoWaybillInfo waybill)
        {
            ITopClient client = new DefaultTopClient(waybill.URL, waybill.Appkey, waybill.Secret);
            CainiaoWaybillIiGetRequest req = new CainiaoWaybillIiGetRequest();
            CainiaoWaybillIiGetRequest.WaybillCloudPrintApplyNewRequestDomain obj1 = new CainiaoWaybillIiGetRequest.WaybillCloudPrintApplyNewRequestDomain();
            obj1.CpCode = waybill.CpCode;
            obj1.CustomerCode = waybill.CustomerCode;
            obj1.ProductCode = waybill.ProductCode;
            obj1.CallDoorPickUp = waybill.CallDoorPickUp;
            CainiaoWaybillIiGetRequest.UserInfoDtoDomain obj2 = new CainiaoWaybillIiGetRequest.UserInfoDtoDomain();
            CainiaoWaybillIiGetRequest.AddressDtoDomain obj3 = new CainiaoWaybillIiGetRequest.AddressDtoDomain();
            obj3.City = waybill.SenderCity;
            obj3.Detail = waybill.SenderDetail;
            obj3.District = waybill.SenderDistrict;
            obj3.Province = waybill.SenderProvince;
            obj2.Address = obj3;
            obj2.Mobile = waybill.SenderMobile;
            obj2.Name = waybill.SenderName;
            obj2.Phone = waybill.SenderPhone;
            obj1.Sender = obj2;
            List<CainiaoWaybillIiGetRequest.TradeOrderInfoDtoDomain> list5 = new List<CainiaoWaybillIiGetRequest.TradeOrderInfoDtoDomain>();
            CainiaoWaybillIiGetRequest.TradeOrderInfoDtoDomain obj6 = new CainiaoWaybillIiGetRequest.TradeOrderInfoDtoDomain();
            list5.Add(obj6);
            obj6.ObjectId = "1";    
            //obj6.LogisticsServices = "{\"RC-S102\":\"\"}";
            CainiaoWaybillIiGetRequest.OrderInfoDtoDomain obj7 = new CainiaoWaybillIiGetRequest.OrderInfoDtoDomain();
            obj7.OrderChannelsType = waybill.OrderChannelsType;
            obj7.TradeOrderList = new List<string>();
            obj7.TradeOrderList.Add(waybill.OrderID);
            obj7.OutTradeOrderList = new List<string>();
            obj7.OutTradeOrderList.Add(waybill.OrderID);
            obj6.OrderInfo = obj7;
            CainiaoWaybillIiGetRequest.PackageInfoDtoDomain obj8 = new CainiaoWaybillIiGetRequest.PackageInfoDtoDomain();
            //obj8.Id = "1";
            List<CainiaoWaybillIiGetRequest.ItemDomain> list10 = new List<CainiaoWaybillIiGetRequest.ItemDomain>();
            foreach (var item in waybill.Items)
            {
                CainiaoWaybillIiGetRequest.ItemDomain obj11 = new CainiaoWaybillIiGetRequest.ItemDomain();
                obj11.Count = item.Count;
                obj11.Name = item.SKU;
                list10.Add(obj11);
            }
           
            obj8.Items = list10;
            obj8.GoodsDescription = waybill.GoodsDescription;
            obj6.PackageInfo = obj8;
            CainiaoWaybillIiGetRequest.RecipientInfoDtoDomain obj12 = new CainiaoWaybillIiGetRequest.RecipientInfoDtoDomain();
            CainiaoWaybillIiGetRequest.AddressDtoDomain obj13 = new CainiaoWaybillIiGetRequest.AddressDtoDomain();
            obj13.City = waybill.ReceiverCity;
            obj13.Detail =waybill.ReceiverDetail;
            obj13.District = waybill.ReceiverDistrict;
            obj13.Province = waybill.ReceiverProvince;
            obj12.Address = obj13;
            obj12.Mobile = waybill.ReceiverMobile;
            obj12.Name = waybill.ReceiverName;
            obj12.Phone = waybill.ReceiverPhone;
            obj12.Oaid = waybill.Oaid;
            obj12.Tid = waybill.Tid;
            obj6.Recipient = obj12;
            obj6.TemplateUrl = waybill.StdTemplateUrl;
            obj6.UserId = waybill.UserID;
            obj1.TradeOrderInfoDtos = list5;
            obj1.BrandCode = waybill.WaybillBrandCode;
            req.ParamWaybillCloudPrintApplyNewRequest_ = obj1;
            CainiaoWaybillIiGetResponse rsp = client.Execute(req, waybill.SessionKey);

            return rsp.Body;
        }
    }

    public class CainiaoWaybillInfo
    {
        public string URL { get; set; }
        public string Appkey { get; set; }
        public string Secret { get; set; }
        public string SessionKey { get; set; }
        public string CpCode { get; set; }
        public string SenderProvince { get; set; }
        public string SenderCity { get; set; }
        public string SenderDistrict { get; set; }
        public string SenderDetail { get; set; }
        public string SenderName { get; set; }
        public string SenderPhone { get; set; }
        public string SenderMobile { get; set; }
        public string ReceiverProvince { get; set; }
        public string ReceiverCity { get; set; }
        public string ReceiverDistrict { get; set; }
        public string ReceiverDetail { get; set; }
        public string ReceiverName { get; set; }
        public string ReceiverPhone { get; set; }
        public string ReceiverMobile { get; set; }
        public long UserID { get; set; }
        public string CustomerCode { get; set; }
        public string CustomTemplateUrl { get; set; }
        public string Oaid { get; set; }
        public string Tid { get; set; }
        public string OrderID { get; set; }
        public string WaybillBrandCode { get; set; }
        public string OrderChannelsType { get; set; }
        public string StdTemplateUrl { get; set; }
        public string ProductCode { get; set; }
        public bool CallDoorPickUp { get; set; }

        public string GoodsDescription { get; set; }

        public List<GoodItem> Items { get; set; }
    }

    public class GoodItem
    {
        public string SKU { get; set; }
        public int Count { get; set; }
    }
}
